/**
 * 初始化部门详情对话框
 */
var DeptInfoDlg = {
    deptInfoData : {},
    zTreeInstance : null,
    officeTreeInstance:null
};

/**
 * 清除数据
 */
DeptInfoDlg.clearData = function() {
    this.deptInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
DeptInfoDlg.set = function(key, val) {
    this.deptInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
DeptInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
DeptInfoDlg.close = function() {
    parent.layer.close(window.parent.Dept.layerIndex);
}

/**
 * 点击部门ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
DeptInfoDlg.onClickDept = function(e, treeId, treeNode) {
	console.log(treeNode.id);
    $("#pName").attr("value", DeptInfoDlg.zTreeInstance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
    DeptInfoDlg.hideDeptSelectTree();
}


//选择公司后的事件
DeptInfoDlg.clickOfficeTree = function(e, treeId, treeNode){
	console.log(treeNode.id);
	
	$("#officeName").attr("value", DeptInfoDlg.officeTreeInstance.getSelectedVal());
    $("#officeId").attr("value", treeNode.id);
    DeptInfoDlg.hideOfficeSelectTree();
   //选择公司之后，动态加载部门树
    DeptInfoDlg.initDeptTree(treeNode.id);
    //不能用$("#pName").val("")置空  否则会出现上级部门选择后无法现实的问题
    $("#pName").attr("value", "");
    $("#pid").attr("value", 0);
    
    
   
    
}

//初始化部门树
DeptInfoDlg.initDeptTree = function(officeId){
	
    var ztree = new $ZTree("parentDeptMenuTree", "/sys/sysDept/tree");
    ztree.bindOnClick(DeptInfoDlg.onClickDept);
    ztree.addParam({"key":"officeId","val":officeId});
    ztree.init();
    DeptInfoDlg.zTreeInstance = ztree;
}


/**
 * 显示部门选择的树
 *
 * @returns
 */
DeptInfoDlg.showDeptSelectTree = function() {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentDeptMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

DeptInfoDlg.showOfficeSelectTree = function() {
    var officeName = $("#officeName");
    var officeNameOffset = $("#officeName").offset();
    $("#officeMenu").css({
        left : officeNameOffset.left + "px",
        top : officeNameOffset.top + officeName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}


/**
 * 隐藏部门选择的树
 */
DeptInfoDlg.hideDeptSelectTree = function() {
    $("#parentDeptMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

DeptInfoDlg.hideOfficeSelectTree = function() {
    $("#officeMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
DeptInfoDlg.collectData = function() {
    this.set('id').set('simplename').set('fullname').set('tips').set('num').set('pid').set('officeId');
}

/**
 * 验证数据非空
 */
DeptInfoDlg.provingNull = function () {
    if(this.get('fullname') && this.get('simplename') && 
    		this.get('pid') && this.get('num') && 
    		this.get('officeId')){  	
    	return true;
    } else {
    	return false;
    }
};

/**
 * 提交添加部门
 */
DeptInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    
    if (!this.provingNull()) {
        Feng.error("请确认必填项");
        return;
    }
    
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/sys/sysDept/save/add", function(data){
    	if(data.resultCode == 1){
    		Feng.success("添加成功!");
            window.parent.Dept.search();
            DeptInfoDlg.close();
            DeptTableToolbar.close();
    	}else{
    		Feng.error(data.msg);
    	}
        
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.deptInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
DeptInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/sys/sysDept/save/update", function(data){
    	if(data.resultCode == 1){
    		Feng.success("修改成功!");
            window.parent.Dept.table.refresh();
            DeptInfoDlg.close();
    	}else{
    		Feng.error(data.msg);
    	}
    	
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
                                          
    ajax.set(this.deptInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "parentDeptMenu" || $(event.target).parents("#parentDeptMenu").length > 0)) {
        DeptInfoDlg.hideDeptSelectTree();
    }
}

$(function() {
    
    var officetree = new $ZTree("officeMenuTree", "/sys/sysDept/offTree");
    officetree.bindOnClick(DeptInfoDlg.clickOfficeTree);
    officetree.init();
    DeptInfoDlg.officeTreeInstance = officetree;
    
    //如果公司不为空，则调用初始化部门的方法
    var officeId = $("#officeId").val();
    console.log(officeId);
    if(officeId){
    	DeptInfoDlg.initDeptTree(officeId);
    } 
});
